/**
 * 
 */
package com.miolr.module.core.service;

import javax.inject.Named;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.miolr.core.service.local.LocalGenericService;
import com.miolr.module.core.dao.MessageDao;
import com.miolr.module.core.model.Message;
import com.miolr.module.core.model.User;

/**
 * @author iday
 * 
 */
@Named
public class MessageService extends LocalGenericService<MessageDao, Message> {

	public Message getLastMessage(String source, User user) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Message.class)
				.add(Restrictions.eq("source", source)).add(Restrictions.eq("creator", user))
				.addOrder(Order.desc("id"));
		return this.dao.findOne(criteria, true);
	}
}
